<template>
    <div class="login" @keydown.enter="handleSubmit">
        <div class="login-con">
            <Card :bordered="false">
                <p slot="title">
                    <Icon type="log-in"></Icon>
                    欢迎登录
                </p>
                <div class="form-con">
                    <Form ref="loginForm" :model="form" :rules="rules">
                        <FormItem prop="userName">
                            <Input v-model="form.userName" placeholder="请输入用户名">
                                <span slot="prepend">
                                    <Icon :size="16" type="md-person"></Icon>
                                </span>
                            </Input>
                        </FormItem>
                        <FormItem prop="password">
                            <Input type="password" v-model="form.password" placeholder="请输入密码">
                                <span slot="prepend">
                                    <Icon :size="14" type="md-lock"></Icon>
                                </span>
                            </Input>
                        </FormItem>
                        <FormItem>
                            <Button @click="handleSubmit" type="primary" long>登录</Button>
                        </FormItem>
                    </Form>
                    <p class="login-tip">输入任意用户名和密码即可</p>
                </div>
            </Card>
        </div>
        <Spin size="large" fix v-if="spinShow"></Spin>
    </div>
</template>

<script>
import { Spin, Card, Form, FormItem, Input, Icon, Button } from 'iview';
export default {
	name:'login',
	components: {Spin, Card, Form, FormItem, Input, Icon, Button},
    data () {
        return {
            spinShow:false,
            form: {
                userName: '15013266010',
                password: '222222'
            },
            rules: {
                userName: [
                    { required: true, message: '账号不能为空', trigger: 'blur' }
                ],
                password: [
                    { required: true, message: '密码不能为空', trigger: 'blur' }
                ]
            }
        };
    },
    methods: {
        handleSubmit () {
			let self=this
            self.spinShow=true
            this.$refs.loginForm.validate((valid) => {
				if (valid) {
					this.$http.post('/oauth2/token',{
						grant_type:'password',
						username:this.form.userName,
						password:this.form.password,
						client_id:'testclient',
						client_secret:'testpass'
					}).then(function(response){

						self.$store.dispatch('setToken',response.data.access_token)
						self.$store.dispatch('setRefreshToken',response.data.refresh_token)
						self.$store.dispatch('setExpires',response.data.expires_in*1000+(new Date()).getTime())
                        window.localStorage.setItem('token',response.data.access_token)
                        window.localStorage.setItem('refreshToken',response.data.refresh_token)
                        window.localStorage.setItem('expires',response.data.expires_in*1000+(new Date()).getTime())

                        
                        //获取用户信息
                        self.$http.get('/v1/members/members/info?token='+response.data.access_token).then(function(response){
                            
						    self.$store.dispatch('setUserInfo',response.data.data)
                            self.$router.push({name:'home'});
                            self.spinShow=false
                        })
                        .catch(function(error){
                            self.spinShow=false
                            self.$Message.error(error.message)
                            return

                        })
                        
					}).catch(function(error){
                        self.spinShow=false
						self.$Message.error(error.message)
                        return

					})
                }
                
			return false
			});
			return false
        }
    }
};
</script>
<style lang="less" scoped>
.login{
    width: 100%;
    height: 100%;
    background-image: url('/static/images/login_bg.jpg');
    background-size: cover;
    background-position: center;
    position: relative;
    &-con{
        position: absolute;
        right: 160px;
        top: 50%;
        transform: translateY(-60%);
        width: 300px;
        &-header{
            font-size: 16px;
            font-weight: 300;
            text-align: center;
            padding: 30px 0;
        }
        .form-con{
            padding: 10px 0 0;
        }
        .login-tip{
            font-size: 10px;
            text-align: center;
            color: #c3c3c3;
        }
    }
}
</style>

